//
// ColourTable
// =============
// A Cinema 4D command plugin, providing a color palette
// By Andreas Block, January 23th 2012
// Mail: open@andreasblock.de
//
// For further information, see colourtable.h
//
//------------------------------------------------------------------------
// File: main.cpp  - plugin startup implementation
//

#include "c4d.h"
#include "c4d_symbols.h"
#include "colourtable.h"

// Forward declarations
Bool RegisterColourTable(void);
C4D_CrashHandler old_handler;

void SDKCrashHandler(CHAR* crashinfo)
{
  // don't forget to call the original handler!!!
  if (old_handler) (*old_handler)(crashinfo);
}

Bool PluginStart(void)
{
  Bool success;

  // example of installing a crashhandler
  old_handler = C4DOS.CrashHandler;     // get the original handler (must be called!)
  C4DOS.CrashHandler = SDKCrashHandler; // insert own handler

  // attempt to register our plugin with C4D; display a message in the console depending on the result
  GePrint(GeLoadString(COLOURTABLE_PLUGIN_NAME) + ":");
  String sVersion = GeLoadString(COLOURTABLE_PLUGIN_VERSION) + ": ";
  sVersion += LongToString(COLOURTABLE_VERSION_MAJOR) + ".";
  sVersion += LongToString(COLOURTABLE_VERSION_MINOR) + ".";
  sVersion += LongToString(COLOURTABLE_VERSION_BUILD);
  GePrint(sVersion);
  if(!RegisterColourTable()) {
    GePrint("  " + GeLoadString(COLOURTABLE_PLUGIN_ERROR));
    success = FALSE;
  } else {
    GePrint("  " + GeLoadString(COLOURTABLE_PLUGIN_SUCCESS));
    success = TRUE;
  }
  GePrint(" ");
  return success;
}

void PluginEnd(void)
{
  // nothing to do here as we haven't allocated any resources which must be freed
  return;
}

Bool PluginMessage(LONG id, void *data)
{
  // Handle incoming messages
  switch (id) {
  case C4DPL_INIT_SYS:
    // Load a resource file(s)
    if (!resource.Init()) {
      return FALSE;
    }
    return TRUE;
    break;
  case C4DMSG_PRIORITY:
    return TRUE;
    break;
  }
  return FALSE; // actually default case
}
